package online.heycm.platform.web.common.sse;

import lombok.Data;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

/**
 * @author heycm
 * @since 2023/12/21 21:33
 */
@Data
public class SseEvent {

    private String id;

    private String event;

    private long retry;

    private String comment;

    private Object data;

    private MediaType mediaType;

    public SseEmitter.SseEventBuilder sseEventBuilder() {
        SseEmitter.SseEventBuilder builder = SseEmitter.event().id(id).name(event).reconnectTime(retry)
                .comment(comment);
        if (mediaType != null) {
            builder.data(data, mediaType);
        } else {
            builder.data(data);
        }
        return builder;
    }
}
